Introduction
The Events Module in xpander.ai SDK provides functionality for handling background tasks and event streaming. It integrates seamlessly with the platform for real-time task execution and monitoring.Overview
The SDK supports comprehensive event handling systems:-
Agent Event Listeners
- Set up using the
@on_task
decorators - Handle incoming task execution requests
- Integrate with Server Sent Events (SSE) for real-time communication
- Includes strict validation to ensure the task parameter is correctly specified and returned
- Set up using the
-
Application Lifecycle Management
- Use
@on_boot
decorators for initialization tasks before event listeners start - Use
@on_shutdown
decorators for cleanup tasks during application shutdown - Support both synchronous and asynchronous functions
- Use
-
Task Event Streaming
- Monitor updates in real-time during task execution
- Track task progress, tool calls, and lifecycle events
Strict Validation
The@on_task
decorator includes comprehensive validation to ensure proper usage and prevent runtime errors:
Parameter Validation
- Required
task
Parameter: The decorated function must have a parameter namedtask
- Task Type Validation: The
task
parameter must be an instance of theTask
class - Return Type Validation: The function must return an instance of the
Task
class - Test Task Validation: If provided,
test_task
must be an instance ofLocalTaskTest
Validation Examples
Valid Function Signature
Invalid Function Signatures (Will Raise Errors)
Examples
Agent Event Listeners
Handle incoming task execution requests for agent deployment.Asynchronous Example
Using@on_task
decorator to create an event handler:
Synchronous Example
Advanced Decorator Usage
The@on_task
decorator supports additional parameters for advanced use cases:
Using Custom Configuration
Using Test Tasks for Local Development
Task Event Streaming
Monitor task execution in real-time.Asynchronous Example
Synchronous Example
Lifecycle Management
Manage application initialization and cleanup with lifecycle decorators:Using @on_boot for Initialization
Using @on_shutdown for Cleanup
Continue to the [Events API Reference](/API reference/events/API reference) for detailed @on_task documentation or see [Lifecycle Decorators](/API reference/events/API reference/lifecycle) for @on_boot and @on_shutdown reference.
Support
For additional help:- Full SDK Documentation
- Email: dev@xpander.ai